package com.lawson.survivor;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider {

	public final String ACTION_SOS = "ACTION_SOS";

	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		final int iWidgets = appWidgetIds.length;

		// Perform this loop procedure for each App Widget that belongs to this provider
		for (int i=0; i<iWidgets; i++) {
			int appWidgetId = appWidgetIds[i];
			RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

			Intent toggleIntent = new Intent(context, WidgetProvider.class);
			toggleIntent.setAction(ACTION_SOS);
			PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, toggleIntent, PendingIntent.FLAG_CANCEL_CURRENT);
			views.setOnClickPendingIntent(R.id.btnWidgetSOSEnabled, pendingIntent);

			// Tell the AppWidgetManager to perform an update on the current app widget
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		final RemoteViews widgetView = new RemoteViews(context.getPackageName(), R.layout.widget);
		
		//TODO is there a better way to manager the context
		final Context widgetContext = context;
		
		if(intent.getAction() != null && intent.getAction().equals(ACTION_SOS)){					
			widgetView.setTextViewText(R.id.textViewWidgetNotification, "Sending SOS...");
			widgetView.setViewVisibility(R.id.btnWidgetSOSNotEnabled, View.VISIBLE);
			widgetView.setViewVisibility(R.id.btnWidgetSOSEnabled, View.GONE);				

			//Refresh the widget layout to reflect visibility changes.
			ComponentName widget = new ComponentName( context, WidgetProvider.class );
			(AppWidgetManager.getInstance(context)).updateAppWidget( widget, widgetView );

			LocationFinder locationFinder = new LocationFinder(context, 30000, 1000, 10);
			locationFinder.setOnLocationFoundListener(new LocationFindListener() {
				@Override
				public void onLocationFound(Location location) {
					sendEmergencyMessage(location);
					widgetView.setTextViewText(R.id.textViewWidgetNotification, "SOS Sent.");
					widgetView.setViewVisibility(R.id.btnWidgetSOSEnabled, View.VISIBLE);
					widgetView.setViewVisibility(R.id.btnWidgetSOSNotEnabled, View.GONE);
					
					//Refresh the widget layout to reflect visibility changes.
					ComponentName widget = new ComponentName(widgetContext, WidgetProvider.class);
					(AppWidgetManager.getInstance(widgetContext)).updateAppWidget( widget, widgetView );
				}
			});	
		}

	}

	private void sendEmergencyMessage(Location location) {
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage("07870339543",null,"TEST", null, null);
	}

}
